package com.example.secondhandmall.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import com.example.secondhandmall.R;
import com.example.secondhandmall.adapter.GoodsAdapter;
import com.example.secondhandmall.adapter.OrderAdapter;
import com.example.secondhandmall.api.Api;
import com.example.secondhandmall.api.ApiConfig;
import com.example.secondhandmall.api.MyCallback;
import com.example.secondhandmall.entity.CollectedEntity;
import com.example.secondhandmall.entity.CollectedListResponse;
import com.example.secondhandmall.entity.GoodsEntity;
import com.example.secondhandmall.entity.GoodsListResponse;
import com.example.secondhandmall.entity.OrderEntity;
import com.example.secondhandmall.entity.OrderListResponse;
import com.example.secondhandmall.util.StringUtils;
import com.google.gson.Gson;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class OrderActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private OrderAdapter orderAdapter;
    private List<OrderEntity> datas;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    orderAdapter.setDatas(datas);
                    orderAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    private ImageView ivReturn;

    @Override
    protected int initLayout() {
        return R.layout.activity_order;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.recyclerView);
        ivReturn = findViewById(R.id.iv_return);
    }

    @Override
    protected void initData() {
        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        orderAdapter = new OrderAdapter(this);
        orderAdapter.setOnItemClickListener(new OrderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int id) {
                navigateTo(DetailActivity.class, "detail", id);
            }
        });
        recyclerView.setAdapter(orderAdapter);

        getOrderList();
//        localOrderList();
    }

    private void getOrderList() {
        String account = getStringFromSp("account");
        if (!StringUtils.isEmpty(account)) {
            HashMap<String, Object> params = new HashMap<>();
            params.put("userId" +
                    "", account);
            Api.config(ApiConfig.OrderList, params).getRequest(new MyCallback() {
                @Override
                public void onSuccess(String res) {
                    OrderListResponse orderListResponse = new Gson().fromJson(res, OrderListResponse.class);
                    if (orderListResponse != null && orderListResponse.getCode() == 200) {
                        datas = orderListResponse.getData();
                        if (datas != null && datas.size() > 0) {
                            mHandler.sendEmptyMessage(0);
                        } else {
                            showToastSync("暂时还没有订单。");
                        }
                    }
                }

                @Override
                public void onFailure(Exception e) {
                    showToastSync("请求失败！");
                }
            });
        } else {
            navigateTo(LoginActivity.class);
            showToast("请先登录");
        }
    }

    private void localOrderList() {
        List<OrderEntity> list = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            OrderEntity orderEntity = new OrderEntity();
            orderEntity.setGoodsName("创意陶瓷杯子");
            orderEntity.setGoodsPrice(24.00);

            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(currentTime);
            orderEntity.setCreatTime(dateString);

            orderEntity.setGoodsUrl("https://img.alicdn.com/bao/uploaded/i1/721058764/O1CN012EbxZp2gZVsjwWS_!!721058764.jpg_200x200q90.jpg_.webp");

            orderEntity.setUserId("诺轩");
//            orderEntity.setUserPhoto("https://img.alicdn.com/bao/uploaded/i1/721058764/O1CN012EbxZp2gZVsjwWS_!!721058764.jpg_200x200q90.jpg_.webp");

//            collectedEntity.setDetail("品牌名称：Nosin/诺轩\n" +
//                    "产品参数：\n" +
//                    "\n" +
//                    "品牌: Nosin/诺轩货号: NS02BB1容量: 201mL(含)-300mL(含)材质: 新骨瓷价格区间: 40元-79.9元风格: 北欧风格主图来源: 自主实拍图产地: 中国大陆是否手工: 是流行元素: 小清新适用人群: 大众适用对象: 成人颜色分类: 锦上添花 可圈可点 编织浪漫");
//            goodsEntity.setCollectFlag(false);
//            goodsEntity.setAddress("广东潮州");

            list.add(orderEntity);
        }
        orderAdapter.setDatas(list);
        orderAdapter.notifyDataSetChanged();
    }
}